import { createRouter, createWebHashHistory,createWebHistory } from 'vue-router'
import variable from "../js/variable";

const title = "阿杜博客"

//路由配置
const routes = [
  {
    meta: { title: title},
    path: '',
    component: () => import('../components/frontdesk/index.vue'),
    children: [
      {
        name: 'Zhongxin',
        path: '',
        component: () => import('../components/frontdesk/Zhongxin.vue'),
      },
      {
        name: 'Article',
        path: 'Article/:id',
        component: () => import('../components/frontdesk/Articles.vue'),
      },
      {
        meta: { title: title+' - 归档' },
        name: 'Archives',
        path: 'Archives',
        component: () => import('../components/frontdesk/Archives.vue'),
      },
      {
        meta: { title: title+' - 关于' },
        name: 'About',
        path: 'About',
        component: () => import('../components/frontdesk/About.vue'),
      },
      {
        meta: { title: title+' - 闲言碎语' },
        name: 'Moments',
        path: 'Moments',
        component: () => import('../components/frontdesk/Moments.vue'),
      },
      {
        meta: { title: title+' - 友联' },
        name: 'Links',
        path: 'Links',
        component: () => import('../components/frontdesk/Links.vue'),
      },
      {
        meta: { title: title+' - 视频解析' },
        name: 'VideoJx',
        path: 'VideoJx',
        component: () => import('../components/frontdesk/VideoJx.vue'),
      },
      {
        meta: { title: title+' - 树洞' },
        name: 'TreeHole',
        path: 'TreeHole',
        component: () => import('../components/frontdesk/TreeHole.vue'),
      },
    ]
  },
  {
    path: '/404',
    component: () => import('../components/Exception.vue')
  },
  {
    path: '/Inhibit',
    component: () => import('../components/Inhibit.vue')
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  },
]

const router = createRouter({
  routes, // 你的路由配置
  history: (() => {
    if (typeof window !== 'undefined' && window.history && window.history.pushState) {
      // 检查浏览器是否支持 HTML5 History API
      return createWebHistory();
    } else {
      // 如果不支持，使用 createWebHashHistory
      return createWebHashHistory();
    }
  })(),
});

router.beforeEach((to, from, next) => {
  //加载动画
  variable.router_loading.value = true

  document.title = to.meta.title || title; // 设置页面标题，如果未设置则使用默认标题
  next(); // 继续导航
});


router.afterEach((to, from) => {
  //结束加载动画
  setInterval(() => {
    variable.router_loading.value = false
  }, 1000);
});

export default router